package sprinklerbee.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import sprinklerbee.dao.SprinklerDAO;
import sprinklerbee.factory.DAOFactory;
import sprinklerbee.factory.ServiceFactory;
import sprinklerbee.model.Group;
import sprinklerbee.model.Scheduler;
import sprinklerbee.model.SchedulerHistory;
import sprinklerbee.model.Sprinkler;

public class SchedulerService{

	public Vector<Vector> getAllSchedules() {
		return transferToVecter(DAOFactory.getSchedulerDAOInstance()
				.queryAllSchedulers());
	}

	public Vector<Vector> getScheduleByGroup(String groupName) {
		return transferToVecter(DAOFactory.getSchedulerDAOInstance()
				.querySchedulersByGroup(groupName));
	}

	public Vector<Vector> getScheduleBySprinkler(String sprinklerName) {
		return transferToVecter(DAOFactory.getSchedulerDAOInstance()
				.querySchedulersBySprinkler(sprinklerName));
	}

	public List<Scheduler> getScheduleRealTime() {
		return DAOFactory.getSchedulerDAOInstance().querySchedulersRealTime();
	}

	public boolean updateSchedule(Scheduler scheduler) {
		return DAOFactory.getSchedulerDAOInstance().updateSchedulerBySprinkler(scheduler);
	}

	public boolean updateAllSchedule(Scheduler scheduler) {
		return DAOFactory.getSchedulerDAOInstance().updateAllSchedulers(scheduler);
	}
	
	public boolean updateSchedule(Group group, Scheduler scheduler) {
		return DAOFactory.getSchedulerDAOInstance().updateSchedulersByGroup(
				group, scheduler);
	}

	public boolean addSchedule(String sprinklerName, Scheduler scheduler) {
		scheduler.setSprinkler(DAOFactory.getSprinklerDAOInstance()
				.querySprinklerByName(sprinklerName));
		return DAOFactory.getSchedulerDAOInstance().addScheduler(scheduler);
	}

	public boolean addScheduleHistory() throws ParseException {
		List<SchedulerHistory> list = new ArrayList<SchedulerHistory>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(new Date());
		for (Scheduler scheduler : DAOFactory.getSchedulerDAOInstance().querySchedulersForToday()) {
			SchedulerHistory history = new SchedulerHistory();
			history.setScheduler(scheduler);
			Date fromTime = format.parse(date + " " + scheduler.getStarttime()
					+ ":00");
			Date toTime = format.parse((date + " " + scheduler.getEndtime())
					+ ":00");

			history.setStarttime(fromTime);
			history.setEndtime(toTime);

			history.setDuration((double) (toTime.getTime() - fromTime.getTime()) / 1000 / 60 / 60);
			history.setConsumption(history.getDuration()
					* Double.parseDouble(ServiceFactory
							.getConfigurationServiceInstance()
							.getConfigurations().get("waterflow").getValue()));
			history.setSprinkler(scheduler.getSprinkler());
			list.add(history);
		}
		return DAOFactory.getSchedulerDAOInstance().addSchedulerConsumption(
				list);
	}

	public boolean delSchedule(Scheduler scheduler) {
		return DAOFactory.getSchedulerDAOInstance().delelteScheduler(scheduler);
	}

	public double getMonthlyConsumption(String month) {
		return DAOFactory.getSchedulerDAOInstance().queryMonthlyConsumption(
				month);
	}

	public List<SchedulerHistory> getAnalysis() {
		return DAOFactory.getSchedulerDAOInstance().querySprinklerConsumption();
	}

	public List<SchedulerHistory> getAnalysisForGroup() {
		return DAOFactory.getSchedulerDAOInstance().queryGroupConsumption();
	}

	public List<SchedulerHistory> getAnalysisByGroup(String groupName) {
		return DAOFactory.getSchedulerDAOInstance().queryGroupConsumption(
				groupName);
	}

	public boolean addSchedulerByGroup(String groupName, Scheduler scheduler) {
		SprinklerDAO sprinklerDAO = DAOFactory.getSprinklerDAOInstance();
		List<Scheduler> list = new ArrayList<Scheduler>();
		List<Sprinkler> sprinklers = sprinklerDAO
				.querySprinklersByGroupId(sprinklerDAO.queryGroupByName(
						groupName).getId());
		for (Sprinkler sprinkler : sprinklers) {
			Scheduler sc = new Scheduler();
			sc.setSprinkler(sprinkler);
			sc.setCreatetime(scheduler.getCreatetime());
			sc.setStarttime(scheduler.getStarttime());
			sc.setEndtime(scheduler.getEndtime());
			sc.setMon(scheduler.isMon());
			sc.setTue(scheduler.isTue());
			sc.setWed(scheduler.isWed());
			sc.setThur(scheduler.isThur());
			sc.setFri(scheduler.isFri());
			sc.setSat(scheduler.isSat());
			sc.setSun(scheduler.isSun());
			list.add(sc);
		}
		return DAOFactory.getSchedulerDAOInstance().addSchedulers(list);
	}

	private Vector<Vector> transferToVecter(List<Scheduler> list) {
		Vector<Vector> vector = new Vector<Vector>();
		for (Scheduler scheduler : list) {
			Vector v = new Vector();
			v.add(scheduler.getId());
			v.add(scheduler.getSprinkler().getName());
			v.add(scheduler.getStarttime());
			v.add(scheduler.getEndtime());
			v.add(scheduler.isMon() ? "Yes" : "No");
			v.add(scheduler.isTue() ? "Yes" : "No");
			v.add(scheduler.isWed() ? "Yes" : "No");
			v.add(scheduler.isThur() ? "Yes" : "No");
			v.add(scheduler.isFri() ? "Yes" : "No");
			v.add(scheduler.isSat() ? "Yes" : "No");
			v.add(scheduler.isSun() ? "Yes" : "No");
			vector.add(v);
		}
		return vector;
	}

}
